<?php
namespace Home\Controller;
use Think\Controller;
class PurchaseController extends ChearkController {
    //直接购买商品
//    public function index(){
//        $shopid = $_REQUEST['id'];//商品id
//        $inforid = session('id');//用户id
//        $time = date("Y-m-d H:i:s",time());//当前时间
//        //shop
//        $data['informatio_id'] = $inforid;
//        $data['J_time'] = $time;
//        $data['shopsid'] = $shopid;
//        $data['f_time'] = $time;
//        $data['zhuangtai'] = "已付款";
//        //提成
//        $wh['home_id'] = $inforid;
////        $wh['home_id'] = 5;
//        $name = M('home')->where($wh)->select();
//        $sid = $name[0]['infor_id'];//上家id
//        $wse['information_id'] = $sid;
//
//        $push = M('purse')->where($wse)->select();
//        if(empty($name)){
//        $res = M('purchase')->add($data);
//        }else{
//            if(empty($push)){
//                $da['information_id'] = $sid;
//                $da['money'] = "1";
//                $da['revenue']="下家贡献一元";
//                M('purse')->add($da);
//            }else{
//                $pu = $push[0]['revenue']."下家贡献一元";
//                $dat['revenue'] = $pu;
//                $dat['money'] = $push[0]['money']+1;
//                M('purse')->where($wse)->save($dat);
//            }
//            $res = M('purchase')->add($data);
//        }
//        if(empty($res)){
//            $this->redirect('Shopping/index', array('cate_id' => 2), 0);
//        }else{
//            $this->redirect('Shopping/index', array('cate_id' => 2), 0);
//        }
//        //这里也是跳到购物车
//    }
    public function ers(){
        $shopid = $_REQUEST['id'];//商品id
        $inforid = session('id');//用户id
//        $whrt['informatio_id'] = $inforid;
//        $whrt['shopsid'] = $shopid;
        $wh['informatio_id'] = $inforid;
        M('purchase')->delete($shopid);
        $sd = M('purchase')->where($wh)->select();
        if(!empty($sd)){
            $this->redirect('Shopping/index', array('cate_id' => 2), 0);
        }else{
            $this->redirect('Main/index', array('cate_id' => 2), 0);
        }
    }
    public function ert(){
        $shopid = $_REQUEST['id'];//商品id
        $ckb = $_REQUEST['ckb'];//地址id
        $price = $_REQUEST['price'];//商品价格
        $inforid = session('id');//用户id
        $time = date("Y-m-d H:i:s",time());//当前时间


        $whert['information_id'] = $inforid;
        $trs =M('purse')->where($whert)->select();
        if($trs[0]['money']<$price){
            $this->redirect('join/money', array('cate_id' => 2), 0);
            return "余额不足，请充值!!!";
        }else{
            $data['informatio_id'] = $inforid;
            $data['shopsid'] = $shopid;

            $dats['informatio_id'] = $inforid;
            $dats['shopsid'] = $shopid;

            $datas['f_time'] = $time;
            $datas['zhuangtai'] = "已付款";
            $datas['address'] = $ckb;

            $data['f_time'] = $time;
            $data['zhuangtai'] = "已付款";

            $wh['home_id'] = $inforid;
            $name = M('home')->where($wh)->select();
            $sid = $name[0]['infor_id'];//上家id

            $wse['information_id'] = $sid;

            $push = M('purse')->where($wse)->select();
            if(empty($name)){
                $ress = M('purchase')->where($dats)->save($datas);
                $mone = $trs[0]['money']-$price;
                $sd['money'] = $mone;
                M('purse')->where($wse)->save($sd);
            }else{
//                if(empty($push)){
//                    $rt['information_id'] = $inforid;
//                    $rt['money'] = 0;
//                    $rt['revenue'] = $time."购买商品一件";
//                    M('purse')->add($rt);
//
//                    $da['information_id'] = $sid;
//                    $da['money'] = "1";
//                    $da['revenue']="下家贡献一元";
//                    M('purse')->add($da);
//                }else{
                    $rts['information_id'] = $inforid;
                    $rt['revenue'] = $push[0]['revenue'].$time."购买商品一件";
                    M('purse')->where($rts)->save($rt);

                    $pu = $push[0]['revenue']."下家贡献一元";
                    $dat['revenue'] = $pu;
                    $dat['money'] = $push[0]['money']+1;
                    M('purse')->where($wse)->save($dat);
            }
            $ress = M('purchase')->where($dats)->save($datas);
            $monee = $trs[0]['money']-$price;
            $sde['money'] = $monee;
            M('purse')->where($wse)->save($sde);
            $this->redirect('join/succes', array('cate_id' => 2), 0);
            return "购买成功，等待发货!!!!";
        }

//        if(empty($ress)){
//            $this->redirect('Shopping/index', array('cate_id' => 2), 0);
//        }else{
//            $this->redirect('Shopping/index', array('cate_id' => 2), 0);
//        }
    }
}